/**
 * Created by jiachenpan on 16/11/18.
 */

export function isExternal(path) {
  return /^(https?:|mailto:|tel:)/.test(path)
}

export function isEmpty(str) {
  if (str === null || str === undefined) {
    return true
  }
  const val = str.trim()
  return val.length === 0
}

export function containEmpty(str) {
  if (str === null || str === undefined) {
    return true
  }
  return str.indexOf(' ') !== -1
}

/* 合法uri*/
export function validateURL(textval) {
  const urlregex = /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/
  return urlregex.test(textval)
}

/* 小写字母*/
export function validateLowerCase(str) {
  const reg = /^[a-z]+$/
  return reg.test(str)
}

/* 大写字母*/
export function validateUpperCase(str) {
  const reg = /^[A-Z]+$/
  return reg.test(str)
}

/* 大小写字母*/
export function validatAlphabets(str) {
  const reg = /^[A-Za-z]+$/
  return reg.test(str)
}

/* 身份证号*/
export function validateIdCard(str) {
  const reg = /\d{17}[\d|xX]|\d{15}/
  return str && (!reg.test(str) || (str.length !== 15 && str.length !== 18))
}

/* 手机号*/
export function validateMobile(value) {
  const reg = /^[1][34578]\d{9}$/
  return value && (!reg.test(value) || !(/^[1-9]\d*$/).test(value) || value.length !== 11)
}

/* 银行卡号*/
export function validateCardNo(value) {
  const reg = /\d{1,}/
  return value && (!reg.test(value)) || (value.length <= 10 || value.length >= 30)
}

/* 邮箱*/
export function validateEmail(value) {
  var reg = /^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/
  return value && (!reg.test(value)) || (value.length < 4 || value.length > 30)
}
